
function qTableKey(state, action) {
    return 's:' + state + ';a:' + action;
}

class Player {
    constructor() {
        this.qTable = {};
    }

    getAction(role, env, eps) {
        let random = Math.random();
        if (random > eps) {
            // 选取价值最高的行动
            let max = -1e12, best = null;
            let actions = env.getAvailableActions();
            for (let i = 0; i < actions.length; i++) {
                let action = actions[i];
                let key = qTableKey(env.getState(role), action);
                let q = this.qTable[key] == undefined ? -1e12 : this.qTable[key];
                if (q > max) {
                    max = q;
                    best = action;
                }
            }
            if (best !== null)
                return best;
            else 
                return this.getRandomAction(env);
        }
        else {
            return this.getRandomAction(env);
        }
    }

    getValueTable() {
        return this.valueTable;
    }

    // 随机选取一个行动
    getRandomAction(env) {
        let actions = env.getAvailableActions();
        return actions[parseInt(Math.random() * actions.length)];
    }
}